<?php


namespace app\applet\controller;


use app\BaseController;
use think\facade\Db;

class Room extends BaseController
{
    public function getGroupList(){
        $community_id=request()->param("community_id");
        $group=Db::table('roominfo')
            ->distinct(true)
            ->field('group')
            ->where("community_id",$community_id)
            ->select();

        parent::sendOutput($group);
    }
    public function getBuildingList(){
        $community_id=request()->param("community_id");
        $group=request()->param("group");
        $building=Db::table('roominfo')
            ->distinct(true)
            ->field('building')
            ->where("community_id",$community_id)
            ->where("group",$group)
            ->orderRaw('CAST(building AS SIGNED) ASC')
            ->select();

        parent::sendOutput($building);
    }
    public function getUnitList(){
        $community_id=request()->param("community_id");
        $group=request()->param("group");
        $building=request()->param("building");
        $unit=Db::table('roominfo')
            ->distinct(true)
            ->field('unit')
            ->where("community_id",$community_id)
            ->where("group",$group)
            ->where("building",$building)
            ->orderRaw('CAST(unit AS SIGNED) ASC')
            ->select();
        parent::sendOutput($unit);
    }
    public function getRoomList(){
        $community_id=request()->param("community_id");
        $group=request()->param("group");
        $building=request()->param("building");
        $unit=request()->param("unit");
        $roomList=Db::table('roominfo')
            ->where("community_id",$community_id)
            ->where("group",$group)
            ->where("building",$building)
            ->where("unit",$unit)
            ->orderRaw('CAST(room AS SIGNED) ASC')
            ->select();
        parent::sendOutput( $roomList);
    }

    //用户绑定房屋
    public function bind(){
        $data=request()->param();
        $openid=$this->request->openid;
        if(empty($data["room_id"])){
            parent::sendOutput("房屋信息错误",500);
        }
        $roominfo=Db::table("roominfo")->where("id",$data["room_id"])->find();
        if(empty($roominfo)){
            parent::sendOutput("房屋信息错误",500);
        }
        //查询用户是否绑定房屋
        $res=Db::table("wxpro_roominfo_user")
            ->where("room_id",$data["room_id"])
            ->where("openid",$openid)
            ->find();
        if(!empty($res)){
            parent::sendOutput("您已绑定该房屋",500);
        }
        //查询用户姓名和手机号是否正确
        if($roominfo["owner_name"]!=$data["name"]){
            parent::sendOutput("姓名不正确",500);
        }
        if($roominfo["owner_mobile"]!=$data["mobile"]){
            parent::sendOutput("手机号码不正确",500);
        }
        $add["openid"]=$openid;
        $add["room_id"]=$data["room_id"];
        $add["community_id"]=$data["community_id"];
        $add["community_name"]=$data["community_name"];
        $add["address"]=$data["address"];
        $add["create_time"]=date("Y-m-d H:i:s");
        $resAdd=Db::table("wxpro_roominfo_user")->insert($add);
        if($resAdd){
            parent::sendOutput("房屋绑定成功");
        }else{
            parent::sendOutput("房屋绑定失败,请重新绑定",500);
        }
        //$add["open"]
        parent::sendOutput($data);
    }


}